//
//  HomeViewController.swift
//  DouYuZB
//
//  Created by 中创 on 2020/4/20.
//  Copyright © 2020 梁森. All rights reserved.
//

import UIKit

let titleH:CGFloat = 40


class HomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setUpUI()
    }
    
    // MARK: 懒加载
    private lazy var titleView:SwiftPageTitleView = { [weak self] in
        let titles = ["推荐", "游戏", "娱乐", "趣玩"]
        let titleY:CGFloat = kStatusBarH + kNavigationBarH
        let titleView = SwiftPageTitleView.init(frame: CGRect(x: 0, y: titleY, width: kScreenWidth, height: titleH), titles: titles)
        titleView.deleate = self
        return titleView
    }()
    private lazy var pageContentView:PageContentView = {    [weak self] in
        let pageY = kStatusBarH + kNavigationBarH + titleH
        let pageH = kScreenHeight - pageY - (self?.tabBarController?.tabBar.frame.size.height)!
        let pageFrame = CGRect(x: 0, y: pageY, width: kScreenWidth, height: pageH)
        var childVCs = [UIViewController]()
        var recommendVC = RecommendViewController()
        childVCs.append(recommendVC)
        var gameVC = GameViewController()
        childVCs.append(gameVC)
        var amuseVC = AmuseViewController()
        childVCs.append(amuseVC)
        childVCs.append(FunnyViewController())
        let pageContentView = PageContentView(frame: pageFrame, childVCs: childVCs, parentVC: self)
        pageContentView.delegate = self
        return pageContentView
    }()

}

// MARK:设置界面相关
extension HomeViewController{
    func setUpUI() {
//        setUpNavigationBar()
        setupTitleView()
        setupContentView()
    }
    // 设置navigationbar
    func setUpNavigationBar() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "logo", highImageName: "", size: CGSize(width: 0, height: 0))
        let size = CGSize(width: 40, height: 40)
        let historyItem = UIBarButtonItem(imageName: "image_my_history", highImageName: "Image_my_history_click", size: size)
        let searchItem = UIBarButtonItem(imageName: "btn_search", highImageName: "btn_search_clicked", size: size)
        let qrCodeItem = UIBarButtonItem(imageName: "Image_scan", highImageName: "Image_scan_click", size: size)
        navigationItem.rightBarButtonItems = [historyItem, searchItem, qrCodeItem]
    }
    // 设置PageTitleView
    func setupTitleView() {
        view.addSubview(titleView)
    }
    // 设置底部页面
    func setupContentView() {
        view.addSubview(pageContentView)
    }
}
// MARK:遵守协议
extension HomeViewController:SwiftPageTitleViewDelegate{
    func pageTitleView(page: SwiftPageTitleView, selectedIndex index: Int) {
        pageContentView.setScrollOffsetX(index: index)
    }
}
extension HomeViewController:PageContentViewDelegate{
    func pageContentView(contentView: PageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        titleView.setScrollIndex(progress: progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}
